package com.sysu.dinner.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.sysu.dinner.model.Manager;
import com.sysu.dinner.model.Restaurant;

@Component("restaurantDao")
public class RestaurantDAO {
	private HibernateTemplate hibernateTemplate;
	
	public int addRestaurant(Restaurant restaurant)
	{
		if(hibernateTemplate.save(restaurant) != null)
			return 1;
		return -1;
	}
	
	public void updateRestaurant(Restaurant restaurant)
	{
		hibernateTemplate.delete(restaurant);
		hibernateTemplate.save(restaurant);
	}
	
	@SuppressWarnings("unchecked")
	public List<Restaurant> getAllRestaurants() {
		return hibernateTemplate.find("from Restaurant r order by r.goodRepNum");
	}
	
	
	@SuppressWarnings("unchecked")
	public Restaurant getRestaurantByManager(int mgrId)
	{
		List<Restaurant> rests = new ArrayList<Restaurant>();
		rests =  hibernateTemplate.find("from Restaurant r where r.manager.id='"+mgrId+"'");
		if(rests.size()>0)
			return rests.get(0);
		return null;
	}
	
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

}
